//
//  KSUserDefaultsTools.swift
//  Supermarket
//
//  Created by liuxm on 2017/8/8.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit

let kAppIsFirstLaunch    = "app_is_first_Launch"
let kUserHaveLogined     = "user_have_logined"
let kUserDefaultsAccount = "user_account"
let kUserToken           = "user_token"
let kUserId              = "user_id"
let kUserMobile          = "user_mobile"
let KUserType            = "user_type"

/// UserDefaults存储数据
class KSUserDefaultsTools: NSObject {
    
    /// app是不是第一次启动
    ///
    /// - Parameter isFirst: true/false
    class func setAppIsFirstLaunch(_ isFirst:Bool) {
        UserDefaults.standard.set(isFirst, forKey: kAppIsFirstLaunch)
        UserDefaults.standard.synchronize()
    }
    class func isAppIsFirstLaunch() ->Bool {
        if UserDefaults.standard.value(forKey: kAppIsFirstLaunch) == nil {
            self.setAppIsFirstLaunch(true)
        }
        
        return UserDefaults.standard.value(forKey: kAppIsFirstLaunch) as! Bool
    }
    
    /// 用户已经登录过
    ///
    /// - Parameter isLogined: true/false
    class func setUserHaveLogined(_ isLogined:Bool) {
        UserDefaults.standard.set(isLogined, forKey: kUserHaveLogined)
        UserDefaults.standard.synchronize()
    }
    class func isUserHaveLogined() ->Bool {
        if UserDefaults.standard.value(forKey: kUserHaveLogined) == nil {
            self.setUserHaveLogined(false)
        }
        return UserDefaults.standard.value(forKey: kUserHaveLogined) as! Bool
    }
    
    /// 记录上次登录的帐户
    ///
    /// - Parameter value: 账户
    class func setAccount(_ value:String) {
        UserDefaults.standard.set(value, forKey: kUserDefaultsAccount)
        UserDefaults.standard.synchronize()
    }
    class func getAccount() ->String {
        if UserDefaults.standard.value(forKey: kUserDefaultsAccount) == nil {
            self.setAccount("")
        }
        return UserDefaults.standard.value(forKey: kUserDefaultsAccount) as! String
    }

    /// 用户登录Token
    ///
    /// - Parameter value: Token
    class func setUserToken(_ value:String) {
        UserDefaults.standard.set(value, forKey: kUserToken)
        UserDefaults.standard.synchronize()
    }
    class func getUserToken() ->String {
        if UserDefaults.standard.value(forKey: kUserToken) == nil {
            self.setUserToken("")
        }
        return UserDefaults.standard.value(forKey: kUserToken) as! String
    }
    
    /// 用户ID
    ///
    /// - Parameter value: UserId
    class func setUserId(_ value:Int) {
        UserDefaults.standard.set(value, forKey: kUserId)
        UserDefaults.standard.synchronize()
    }
    class func getUserId() ->Int {
        return UserDefaults.standard.value(forKey: kUserId) as! Int
    }
    
    /// 手机号码
    ///
    /// - Parameter value: UserId
    class func setUserMobile(_ value:String) {
        UserDefaults.standard.set(value, forKey: kUserMobile)
        UserDefaults.standard.synchronize()
    }
    class func getUserMobile() ->String {
        return UserDefaults.standard.value(forKey: kUserMobile) as! String
    }
    
    /// 用户类型
    ///
    /// - Parameter value: UserType 1经销商 2商家
    class func setUserType(_ value:Int) {
        UserDefaults.standard.set(value, forKey: KUserType)
        UserDefaults.standard.synchronize()
    }
    class func getUserType() ->Int {
        if UserDefaults.standard.value(forKey: KUserType) == nil {
            self.setUserType(0)
        }
        return UserDefaults.standard.value(forKey: KUserType) as! Int
    }
}
